/*
 *
 * Copyright 2003, 2004 Blur Studio Inc.
 *
 * This file is part of the Resin software package.
 *
 * Assburner is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Assburner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
 
#include <qapplication.h>
#include <qdir.h>
#include <qfile.h>

#include "blurqt.h"
#include "mainwindow.h"
#include "schema.h"
#include "sourcegen.h"

static void usage()
{
	LOG_5( "--help, -h\tPrints the command line options" );
	LOG_5( "--schema, -s FILE\tLoads the schema from file FILE" );
	LOG_5( "--output, -o DIR\tOutputs C++ code generated by the loaded schema to DIR/autocore and DIR/autoimp" );
	LOG_5( stoneOptionsHelp() );
}

int main( int argc, char * argv[] )
{
	
#ifdef Q_WS_X11
	bool useGUI = getenv( "DISPLAY" ) != 0;
#else
	bool useGUI = true;
#endif // Q_WS_X11
	QApplication a( argc, argv, useGUI );

	if( !initConfig( "classmaker.ini" ) )
        return -1;

	Schema * schema = 0;
	QString fileName;

	for( int i=1; i<argc; i++ )
	{
		QString arg( argv[i] );
		if( arg == "--schema" || arg == "-s"  ){
			if( i+1==argc ) { usage(); return 1; }
			fileName = QString( argv[i+1] );
			if( i<argc && QFile::exists( fileName ) ){
				schema = Schema::createFromXmlSchema( fileName, /*isfilename=*/true, /*ignoreDocs=*/false );
			}else{
				LOG_1( "File not found: " + fileName );
				return 1;
			}
			i++;
		}
		else if( arg == "--output" || arg == "-o" ) {
			if( i+1==argc ) { usage(); return 1; }
			if( !schema )
				LOG_1( "Must first specify the schema to load with --schema" );
			else if( i >= argc || !QDir( argv[i+1] ).exists() )
				LOG_1( "Output Directory not found" );
			else {
				writeSource( schema, argv[i+1] );
				return 0;
			}
			return 1;
		} else if ( arg == "--help" || arg == "-h" ) {
			usage();
			return 1;
		}
	}
	
	initStone( argc, argv );

	if( useGUI ) {
		MainWindow * mw = new MainWindow( schema );
		if( !fileName.isEmpty() )
			mw->setFileName( fileName );
		if( schema )
			mw->setSchema( schema );
		mw->show();
		int i = a.exec();
		shutdown();
		return 0;
	} else {
		qWarning( "Classmaker usage without xserver is limited to the -s -o options" );
	}
	shutdown();
	return 0;
}

